/*******************************
 * 派乐科技馆
 * NXT温度传感器(LM35)
 * 连接：LM35 - A0
 * 说明：可直接用EV3上的温度传感器模块
 * 时间：2019.09.15
 * 修改：2021.01.12
 *******************************/

#include <SoftwareSerial.h>
#include <EV3UARTEmulation.h>
EV3UARTEmulation sensor(3, 4, 6, 38400);

unsigned long last_reading = 0;
float TempC;

void setup() 
{
  pinMode(13, OUTPUT);
  sensor.create_mode("TempC", true, DATA16, 1, 3, 2);
  sensor.create_mode("TempF", true, DATA16, 1, 3, 2);
  sensor.reset();
  Serial.begin(9600);
  digitalWrite(13, HIGH);
}

void loop() 
{
  sensor.heart_beat();
  if (millis() - last_reading > 100) 
  {
    int val = analogRead(A0);
    //TempC = val*5/10.24;
    TempC = 25.0;
    float Temp_send_C = 1000 / 3.91 * TempC;
    switch (sensor.get_current_mode()) 
    {
      case 0:
        sensor.send_data16(0, Temp_send_C);
        break;
      case 1:
        sensor.send_data16(1, Temp_send_C);
        break;
    }
    last_reading = millis();
  }
}
